﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace VSWClient
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {		
		protected override void OnStartup(StartupEventArgs e)
		{
			try
			{
				base.OnStartup(e);
				EventManager.RegisterClassHandler(typeof(DatePicker),
					DatePicker.LoadedEvent,
					new RoutedEventHandler(DatePicker_Loaded));
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
		void DatePicker_Loaded(object sender, RoutedEventArgs e)
		{
			var dp = sender as DatePicker;
			if (dp == null) return;

			var tb = GetChildOfType<DatePickerTextBox>(dp);
			if (tb == null) return;

			var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
			if (wm == null) return;
			wm.Content = "select a date";
		}
		public static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
		{
			if (depObj == null) return null;

			for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
			{
				var child = VisualTreeHelper.GetChild(depObj, i);

				var result = (child as T) ?? GetChildOfType<T>(child);
				if (result != null) return result;
			}
			return null;
		}
	}
}
